    package com.elite.groovy.callback

import com.elite.groovy.util.EliteBean
    import com.elite.groovy.util.Util
    import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

    //导入grooby的json解析api包

/**
     *
     * @author luper
     *
     */

        public class GwZalOrder implements ServiceBackInvoke{
        public static final Log log = LogFactory.getLog(GwZalOrder.class);
        private String charset = "gbk"; //设置字符集
        private int timeout = 100000; //设置请求超时时间
        private int enableSSLflag = 1;
        private String casecharset = "utf-8";
        private int caseenableSSLflag = 1;

        public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response){
                log.debug("-----GwZalOrder Begin Systeam----------");
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");

            String requestJSONStr = Util.getRequestBody(request, "UTF-8");
                log.debug("requestJSONStr:" + requestJSONStr);

            request.getParameter("interfacetype");//获取接口请求类型：create 爱老服务工单新建；/search 爱老服务工单查询
            String v_interfacetype=request.getParameter("interfacetype");
                log.debug("v_interfacetype:"+v_interfacetype);
            eliteBean.update("insert into INTERFACE_JSONBODY values(sysdate,'8099', 'GwZalOrder','"+requestJSONStr.toString()+"',sysdate,null)","dataSource");

            //爱老服务工单新建
            if(v_interfacetype=='create'){
                JSONObject json  = new JSONObject().fromObject(requestJSONStr);
                String customername = json.getString("customername"); //客户姓名
                String mobile = json.getString("mobile"); //客户电话

                if(customername == null || customername=='') {
                    JSONObject jsonALL3 = new JSONObject();
                    jsonALL3.put("retCode", "-1");
                    jsonALL3.put("retMessage", "ELITETASK！");
                    return jsonALL3;
                }
                if(mobile == null || mobile=='') {
                    JSONObject jsonALL3 = new JSONObject();
                    jsonALL3.put("retCode", "-1");
                    jsonALL3.put("retMessage", "客户电话不能为空！");
                    return jsonALL3;
                }

                //生成objective_guid  工单号
                def querysql11="select sysguid objective_guid,'ALFW'||to_char(sysdate,'YYYYMMDDhh24mi')||(case when SEQ_GWZTSORDER.nextval>=1 and SEQ_GWZTSORDER.nextval<10 then '000'||SEQ_GWZTSORDER.nextval  when SEQ_GWZTSORDER.nextval>=10 and SEQ_GWZTSORDER.nextval<100  then '00'||SEQ_GWZTSORDER.nextval  when SEQ_GWZTSORDER.nextval>=100 and SEQ_GWZTSORDER.nextval<1000  then '0'||SEQ_GWZTSORDER.nextval else to_char(SEQ_GWZTSORDER.nextval)end) workorderid from dual";
                def list11=eliteBean.chaxun_sql(querysql11,"dataSource");
                    log.debug("list11-------------------------------"+list11);
                def map11=list11[0];
                    log.debug("map11-------------------------------"+map11);
                def v_objective_guid=map11.get("objective_guid");
                def v_workorderid=map11.get("workorderid");
                    log.debug("v_objective_guid-------------------------------"+v_objective_guid);
                    log.debug("v_workorderid-------------------------------"+v_workorderid);

                //新老客户检查
                def querysql1="select count(*) count1 from customer where customername='"+customername+"' and mobile='"+mobile+"'";
                def list1=eliteBean.chaxun_sql(querysql1,"dataSource");
                    log.debug("list1-------------------------------"+list1);

                for(def k=0;k<list1.size();k++){
                    def map1=list1[k];
                    def v_count1=map1.get("count1");
                        log.debug("v_count1---v_count1-----v_count1----"+v_count1);

                    //新客户新建爱老服务工单
                    if(v_count1==0){
                        //新customer_guid
                        def querysql2="select sysguid new_customer_guid from dual";
                        def list2=eliteBean.chaxun_sql(querysql2,"dataSource");
                            log.debug("list2-------------------------------"+list2);
                        def map2=list2[0];
                            log.debug("map2-------------------------------"+map2);
                        def v_customer_guid=map2.get("new_customer_guid");
                            log.debug("v_customer_guid-------------------------------"+v_customer_guid);

                        //生成 customer
                        def updatesql3="insert into customer(CUSTOMER_GUID,customertype_id,CUSTOMERNAME,MOBILE,WORKSTATUS)select '"+v_customer_guid+"','YYjoin','"+customername+"','"+mobile+"','ONLINE' FROM dual";
                        def updatesqlresult3=eliteBean.update(updatesql3,"dataSource");
                            log.debug("updatesqlresult3:"+updatesqlresult3);

                        //生成 objective
                        def updatesql4 = "insert into objective(OBJECTIVE_GUID, OBJECTIVETYPE_ID,HANDLEGROUP_ID,PRIORITY_ID, EXPIREDDATE, CUSTOMER_GUID,OBJECTIVESTATUS,CREATEDBY, CREATEDDATE,WORKORDERFRAME_ID,CATEGORY,OBJECTIVESUBSTATUS,MAP_ID,CURSTEP_ID_MAP,QA_FLAG,ibcallname,ibcalltel,REOPEN,workorderid,SOURCE,ispolicy,callby,calltel,handlerole_id,realvisiter_flag,callstarttime)values('" + v_objective_guid + "','D72LC5','ORG016','NORMAL',sysdate+5,'" + v_customer_guid + "','OPEN','kf6089',sysdate,'PN1LBE','WORDER',null,'PN1LBE','OESLNM','0','" + customername + "','" + mobile + "','0','" + v_workorderid + "','INTERFACE','1','" + customername + "','" + mobile + "',null,0,sysdate)";
                        def updatesqlresult4 = eliteBean.update(updatesql4, "dataSource");
                        log.debug("updatesqlresult4:" + updatesqlresult4);

                        //生成 tabc
                        def updatesql6="insert into tabcorder_alfw(RELATION_ID,workroderid) values('"+v_objective_guid+"','"+v_workorderid+"')";
                        def updatesqlresult6=eliteBean.update(updatesql6,"dataSource");
                        log.debug("updatesqlresult6:"+updatesqlresult6);

                        //生成 elitetask
                        def updatesql5="insert into elitetask(ELITETASK_GUID,OBJECTIVE_GUID,OBJECTIVETYPE_ID,EXPECTSTARTTIME,EXPECTENDTIME,PRIORITY,HANDLEGROUP_ID,CUSTOMER_GUID,CURSTEPSTATUS,STEPFORECASTDATE,CURSTEP_ID_MAP, MAP_ID, OBJECTIVESUBSTATUS,OBFLAG,handlerole_id)values(sysguid,'"+v_objective_guid+"','D72LC5',sysdate,sysdate,'NORMAL','ORG016','"+v_customer_guid+"','OPEN',sysdate,'OESLNM','PN1LBE',null,'0',null)";
                        def updatesqlresult5=eliteBean.update(updatesql5,"dataSource");
                        log.debug("updatesqlresult5:"+updatesqlresult5);

                        //返回成功结果
                        JSONObject jsonALL3 = new JSONObject();
                        jsonALL3.put("retCode", "1");
                        jsonALL3.put("retMessage", "success");
                        jsonALL3.put("Workorderid", v_workorderid);
                        return jsonALL3;
                    }

                    //老客户新建爱老信息工单
                    if(v_count1>0){
                        //查已有的customer_guid
                        def querysql7="select customer_guid customer_guid from customer where customername='"+customername+"' and mobile='"+mobile+"' and rownum=1";
                        def list7=eliteBean.chaxun_sql(querysql7,"dataSource");
                        log.debug("list7-------------------------------"+list7);
                        def map7=list7[0];
                        log.debug("map7-------------------------------"+map7);
                        def v_customer_guid=map7.get("customer_guid");
                        log.debug("v_customer_guid-------------------------------"+v_customer_guid);

                        //生成 objective
                        def updatesql4 = "insert into objective(OBJECTIVE_GUID, OBJECTIVETYPE_ID,HANDLEGROUP_ID,PRIORITY_ID, EXPIREDDATE, CUSTOMER_GUID,OBJECTIVESTATUS,CREATEDBY, CREATEDDATE,WORKORDERFRAME_ID,CATEGORY,OBJECTIVESUBSTATUS,MAP_ID,CURSTEP_ID_MAP,QA_FLAG,ibcallname,ibcalltel,REOPEN,workorderid,SOURCE,ispolicy,callby,calltel,handlerole_id,realvisiter_flag,callstarttime)values('" + v_objective_guid + "','D72LC5','ORG016','NORMAL',sysdate+5,'" + v_customer_guid + "','OPEN','kf6089',sysdate,'PN1LBE','WORDER',null,'PN1LBE','OESLNM','0','" + customername + "','" + mobile + "','0','" + v_workorderid + "','INTERFACE','1','" + customername + "','" + mobile + "',null,0,sysdate)";
                        def updatesqlresult4 = eliteBean.update(updatesql4, "dataSource");
                        log.debug("updatesqlresult4:" + updatesqlresult4);

                        //生成 tabc
                        def updatesql6="insert into tabcorder_alfw(RELATION_ID,workroderid) values('"+v_objective_guid+"','"+v_workorderid+"')";
                        def updatesqlresult6=eliteBean.update(updatesql6,"dataSource");
                        log.debug("updatesqlresult6:"+updatesqlresult6);

                        //生成 elitetask
                        def updatesql5="insert into elitetask(ELITETASK_GUID,OBJECTIVE_GUID,OBJECTIVETYPE_ID,EXPECTSTARTTIME,EXPECTENDTIME,PRIORITY,HANDLEGROUP_ID,CUSTOMER_GUID,CURSTEPSTATUS,STEPFORECASTDATE,CURSTEP_ID_MAP, MAP_ID, OBJECTIVESUBSTATUS,OBFLAG,handlerole_id)values(sysguid,'"+v_objective_guid+"','D72LC5',sysdate,sysdate,'NORMAL','ORG016','"+v_customer_guid+"','OPEN',sysdate,'OESLNM','PN1LBE',null,'0',null)";
                        def updatesqlresult5=eliteBean.update(updatesql5,"dataSource");
                        log.debug("updatesqlresult5:"+updatesqlresult5);
                        //返回成功结果
                        JSONObject jsonALL3 = new JSONObject();
                        jsonALL3.put("retCode", "1");
                        jsonALL3.put("retMessage", "success");
                        jsonALL3.put("Workorderid", v_workorderid);
                        return jsonALL3;
                    }

                    //返回失败结果
                    else{
                        JSONObject jsonALL3 = new JSONObject();
                        jsonALL3.put("retCode", "-1");
                        jsonALL3.put("retMessage", "failure");
                        return jsonALL3;
                    }
                }
            }
        }
    }
